package com.huangj.fragments;

import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.huangj.adapter.HomeListViewAdapter;
import com.huangj.adapter.HomePicsAdapter;
import com.huangj.entity.HomeEntity;
import com.huangj.entity.HomePics;
import com.huangj.huangjlibrary.base.BaseFragment;
import com.huangj.huangjlibrary.util.DownUtil;
import com.huangj.pro2_h2pop.R;
import com.huangj.util.Constant;
import com.huangj.util.JsonUtil;

import java.util.List;

import butterknife.BindView;


public class HomeFragment extends BaseFragment implements DownUtil.OnDownListener {

    private HomePicsAdapter homePicsAdapter;
    private ViewPager vp_pic;
    private HomeListViewAdapter homeListViewAdapter;
    @BindView(R.id.lv_home)
    ListView lv_home;

    @Override
    public int getContentId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void init(View view) {
        View headview = LayoutInflater.from(getActivity()).inflate(R.layout.headview_home,null);
        vp_pic = (ViewPager) headview.findViewById(R.id.vp_pic);
        homeListViewAdapter = new HomeListViewAdapter(getActivity());
        lv_home.setAdapter(homeListViewAdapter);
        lv_home.addHeaderView(headview);
    }

    @Override
    protected void loadDatas() {
        /**
         *  解析头部图片；
         */
        new DownUtil().setOnDownListener(new DownUtil.OnDownListener() {
            @Override
            public Object paresJson(String json) {
                if (json!=null){
                    return JsonUtil.getHomePicsJson(json);
                }
                return null;
            }

            @Override
            public void downSucc(Object object) {
                List<HomePics> pics = (List<HomePics>) object;
                homePicsAdapter = new HomePicsAdapter(getChildFragmentManager(),pics);
                vp_pic.setAdapter(homePicsAdapter);
            }
        }).downJSON(Constant.URL_HOME);

        //下载首页数据；
        new DownUtil().setOnDownListener(this).downJSON(Constant.URL_GL);
    }

    @Override
    public Object paresJson(String json) {
        if (json != null){
            return JsonUtil.getHomeEntityJson(json);
        }
        return null;
    }

    @Override
    public void downSucc(Object object) {
        if (object != null){
            HomeEntity homeEntity = (HomeEntity) object;
            List<HomeEntity.DataBean> datas = homeEntity.getData();
            homeListViewAdapter.setDatas(datas);
        }
    }
}
